﻿// CheckModelEditDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckModelEditDialog.h"
#include "afxdialogex.h"


// CCheckModelEditDialog 对话框

IMPLEMENT_DYNAMIC(CCheckModelEditDialog, CDialogEx)

CCheckModelEditDialog::CCheckModelEditDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MODEL_EDIT, pParent)
	, m_strCheckElement(_T(""))
{
	m_pModel = NULL;
}

CCheckModelEditDialog::~CCheckModelEditDialog()
{
	if (m_pModel)
	{
		m_pModel->AddRef();
		m_pModel = NULL;
	}
}

void CCheckModelEditDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_MODEL, m_tree);
	DDX_Text(pDX, IDC_EDIT_CHECK_ELEMENT, m_strCheckElement);
	DDX_Control(pDX, IDC_CHECK_OPERATOR_ALL, m_chkOperator);
	DDX_Control(pDX, IDC_LIST_OPERATOR, m_list);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_props);
}


BEGIN_MESSAGE_MAP(CCheckModelEditDialog, CDialogEx)
	ON_WM_SIZE()
	//ON_NOTIFY(NM_CLICK, IDC_TREE_MODEL, &CCheckModelEditDialog::OnNMClickTreeModel)
	//ON_NOTIFY(NM_DBLCLK, IDC_TREE_MODEL, &CCheckModelEditDialog::OnNMDblclkTreeModel)
	//ON_NOTIFY(NM_RCLICK, IDC_TREE_MODEL, &CCheckModelEditDialog::OnNMRClickTreeModel)
	ON_BN_CLICKED(IDOK, &CCheckModelEditDialog::OnBnClickedOk)
END_MESSAGE_MAP()


// CCheckModelEditDialog 消息处理程序


BOOL CCheckModelEditDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	//CRect rect;
	//this->GetWindowRect(rect);
	//this->ScreenToClient(rect);

	//int margin = abs(rect.left);
	//rect.left = margin;
	//rect.top = margin;
	//rect.right -= 2*margin;
	//rect.bottom -= 2 * margin;

	
	AdjustLayout();


	m_tree.AttachBitmap();
	m_tree.SetModel(m_pModel);
	m_tree.Populate(FALSE);

	m_list.Init2();

	m_props.Init2();
	

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CCheckModelEditDialog::SetModel(ck::CheckModel	*pModel)
{
	m_pModel = pModel;
	if (m_pModel)
	{
		m_pModel->AddRef();
	}
}


void CCheckModelEditDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	AdjustLayout();
	
	//if (GetDlgItem(IDC_EDIT1)->GetSafeHwnd())
	//{
	//	CRect rect;
	//	this->GetWindowRect(rect);
	//	this->ScreenToClient(rect);

	//	int margin = abs(rect.left);
	//	rect.left = margin;
	//	rect.top = margin;
	//	rect.right -= 2 * margin;
	//	rect.bottom -= 2 * margin;

	//	GetDlgItem(IDC_EDIT1)->MoveWindow(rect);
	//}
	
	// TODO: 在此处添加消息处理程序代码
}


void CCheckModelEditDialog::AdjustLayout()
{
	if (!m_tree.GetSafeHwnd())
	{
		return;
	}

	CRect rect;
	this->GetWindowRect(rect);
	this->ScreenToClient(rect);
		
	int margin = abs(rect.left);
	int span = margin /2;

	rect.left = margin;
	rect.top = margin;
	rect.right -= 2 * margin;
	rect.bottom -= 2 * margin;

	//////////////////////////////////////////////////////////////////////////
	// 按钮
	//////////////////////////////////////////////////////////////////////////
	CRect rcBtnCanl;
	GetDlgItem(IDCANCEL)->GetWindowRect(rcBtnCanl);
	this->ScreenToClient(rcBtnCanl);
	int btnWidth = rcBtnCanl.Width();
	int btnHeight = rcBtnCanl.Height();
	rcBtnCanl.right = rect.right;
	rcBtnCanl.bottom = rect.bottom-margin;
	rcBtnCanl.left = rcBtnCanl.right - btnWidth;
	rcBtnCanl.top = rcBtnCanl.bottom - btnHeight;
	GetDlgItem(IDCANCEL)->MoveWindow(rcBtnCanl);

	CRect rcBtnOK;
	GetDlgItem(IDOK)->GetWindowRect(rcBtnOK);
	this->ScreenToClient(rcBtnOK);
	rcBtnOK.right = rcBtnCanl.left - (2 * margin);
	rcBtnOK.bottom = rcBtnCanl.bottom;
	rcBtnOK.left = rcBtnOK.right - btnWidth;
	rcBtnOK.top = rcBtnCanl.top;
	GetDlgItem(IDOK)->MoveWindow(rcBtnOK);

	//////////////////////////////////////////////////////////////////////////
	// 树控件(左）
	//////////////////////////////////////////////////////////////////////////
	CRect rcTree;
	m_tree.GetWindowRect(rcTree);
	this->ScreenToClient(rcTree);
	rcTree.bottom = rcBtnCanl.top - margin;
	m_tree.MoveWindow(rcTree);

	//////////////////////////////////////////////////////////////////////////
	// 属性控件(中）
	//////////////////////////////////////////////////////////////////////////
	CRect rcStatic;
	GetDlgItem(IDC_STATIC_GRID)->GetWindowRect(rcStatic);
	this->ScreenToClient(rcStatic);
	int gridWidth = rcStatic.Width();
	rcStatic.left = rcTree.right + span;
	rcStatic.top = rcTree.top;
	rcStatic.right = rcStatic.left + gridWidth;
	rcStatic.bottom = rcTree.bottom;
	GetDlgItem(IDC_STATIC_GRID)->MoveWindow(rcStatic);

	CRect rcGrid;
	rcGrid.left = rcStatic.left + 1;
	rcGrid.top = rcStatic.top + 1;
	rcGrid.right = rcStatic.right - 1;
	rcGrid.bottom = rcStatic.bottom - 1;
	m_props.MoveWindow(rcGrid);

	//////////////////////////////////////////////////////////////////////////
	// 列表控件(右）
	//////////////////////////////////////////////////////////////////////////

	CRect rcItem;
	GetDlgItem(IDC_STATIC_CHECK_ITEM)->GetWindowRect(rcItem);
	this->ScreenToClient(rcItem);
	int itemWidth = rcItem.Width();

	CRect rcEdit;
	GetDlgItem(IDC_EDIT_CHECK_ELEMENT)->GetWindowRect(rcEdit);
	this->ScreenToClient(rcEdit);
	int editWidth = rcEdit.Width();
	int editHeight = rcEdit.Height();

	rcItem.left = rcStatic.right + span;
	rcItem.right = rcItem.left + itemWidth;
	rcItem.top = rcStatic.top+2;
	rcItem.bottom = rcStatic.top + editHeight;
	GetDlgItem(IDC_STATIC_CHECK_ITEM)->MoveWindow(rcItem);

	rcEdit.left = rcItem.right + span;
	rcEdit.right = rcEdit.left + editWidth;
	rcEdit.top = rcStatic.top;
	rcEdit.bottom = rcEdit.top + editHeight;
	GetDlgItem(IDC_EDIT_CHECK_ELEMENT)->MoveWindow(rcEdit);

	
	CRect rcChk;
	m_chkOperator.GetWindowRect(rcChk);
	this->ScreenToClient(rcChk);
	int chkWidth = rcChk.Width();
	int chkHeight = rcChk.Height();
	rcChk.left = rcEdit.right + 2*span;
	rcChk.right = rcChk.left + chkWidth;
	rcChk.top = rcStatic.top;
	rcChk.bottom = rcChk.top + editHeight;
	m_chkOperator.MoveWindow(rcChk);

	CRect rcList;
	m_list.GetWindowRect(rcList);
	this->ScreenToClient(rcList);
	rcList.left = rcItem.left;
	rcList.right = rect.right ;
	rcList.top = rcItem.bottom + span;
	rcList.bottom = rcTree.bottom;
	m_list.MoveWindow(rcList);
}

void CCheckModelEditDialog::UpdateCheckElementText(CString strText)
{
	m_strCheckElement = strText;
	UpdateData(FALSE);
}

void CCheckModelEditDialog::UpdateCheckItemGrid(ck::CheckItem* pCheckItem)
{
	m_props.Populate(pCheckItem);
}

void CCheckModelEditDialog::PopulateOperators(ck::CheckItem* pCheckItem)
{
	BOOL isShowAll = ((CButton*)GetDlgItem(IDC_CHECK_OPERATOR_ALL))->GetCheck();

	m_list.Populate(pCheckItem->GetCode(), isShowAll);
}

void CCheckModelEditDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}
